﻿package component.tree.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import component.tree.entity.Tree;
import component.tree.model.TreeModel;
import component.tree.sql.TreeSqlFactory;

public class TreeDAO extends ComponentDAO implements ITreeDAO {
	
	/**
	 * 删除节点本身以及其所有子节点
	 */
	public boolean delete(String[] fidArr,TreeModel treeModel) {
		// TODO Auto-generated method stub
		
		String fpathName = treeModel.getFpathName();
		
		IConnectionProvider connectionProvider = ConnectionProviderFactory.getConnectionProvider();
		Connection con = connectionProvider.getConnection();
		
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try{
			for(int i = 0;i<fidArr.length;i++){
				String fid = fidArr[i];
				String sql = "delete from " + treeModel.getTableName() + " where " + treeModel.getFpathName() + " like %" + fid + "%";
				pstm = con.prepareStatement(sql);
				pstm.addBatch();
			}
			
			int[] size = pstm.executeBatch();
			System.out.println("删除了 " + size.length + " 条记录！");
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			connectionProvider.close(rs, pstm);
		}
		
		
		
		return false;
	}
	
	/**
	 * 保存
	 * 保存后再更新path
	 */
	public String save(Tree tree,TreeModel treeModel) {
		// TODO Auto-generated method stub
		
		Tree parent = tree.getParent();
		String parentPath = parent.getPath();
		
		String insertSql = ""; 
		
		return null;
	}

	/**
	 * 根据父节点id查找所有子节点
	 */
	public List findTreeDataByParentId(String parentId,TreeModel treeModel) {
		// TODO Auto-generated method stub
		
		String childSql = TreeSqlFactory.getChildSql(parentId, treeModel);
		List list = this.findDataBySql(childSql, new Tree());
		
		return list;
	}
	
	
}
